
//记录查询出的所有用户信息
var siteTextobj=new Array();

//记录点击当前行的用户ID 修改用户信息使用
var siteTextGridnwxTextId;

var siteTextUserId=0;

function siteTextGridColumnsRenderRolelink(nwxTextId,sname){
	siteTextGridnwxTextId=nwxTextId;
	showsiteTextRoleForm(nwxTextId,sname);
}
function siteTextGridColumnsRenderdellink(nwxTextId,sname){
	
	Ext.MessageBox.confirm('信息', '是否确认删除菜单： '+sname+'?', function(btn){
		
		if(btn=='yes'){
			Ext.Ajax.request({
				url:'delAdminSiteText.do',
				method:'POST',
				params:{
					ids:nwxTextId
				},
				success:function(response){
					if(response.responseText=='1'){
						Ext.MessageBox.alert('信息','删除成功！');
						siteTextGridStore.reload();
					}else{
						Ext.MessageBox.alert('ERROR','删除失败！');
					}
				},
				failure:function(){
					Ext.Msg.alert('ERROR','错误');
				}
			});
		}
	});
}

function siteTextGridColumnsRenderInfolink(nwxTextId){
	siteTextGridnwxTextId=nwxTextId;
	if(siteTextGridnwxTextId==null)return ;
	showsiteTextAddForm(2,siteTextobj[nwxTextId]);
}

var siteTextAddWin;
var siteTextTitle=new Ext.form.TextField({
		labelAlign:'right',
		width:390,
		fieldLabel: '文章标题'
});
var siteTextnisValid=new Ext.form.Checkbox({
		fieldLabel:'是否有效',
		labelAlign:'right',
		checked:true
});

var siteTextnckeditor=new Ext.form.CKEditor({  
	    xtype : 'ckeditor',  
	    name : 'content',  
	    width : 200,  
	    height : 300  
}); 

//tinfo参数值1 :添加 2:修改
function showsiteTextAddForm(tinfo,siteTextJson) {
	
	if(!siteTextAddWin){
		siteTextAddform = new Ext.FormPanel({
			fileUpload: true,
		    border: false,
		    labelAlign:'right',
		    frame:true,
		    layout:'column',
		    items: [
			    {layout:'form',labelAlign:'right',items:[
			    		siteTextnisValid,siteTextTitle
			    	]},siteTextnckeditor
		    ],
		    buttons: [{
		        text: tinfo,
		        handler: function() {
		        	if(this.text=='添 加'){
		        		 siteTextAddform.getForm().submit({
		                    url: 'saveAdminSiteText.do',
		                    method:'POST',
		                    waitMsg: 'Uploading your data...',
		                    params : {
		                    	userid:siteTextUserId,
		                    	sTitle:siteTextTitle.getValue(),
		                    	sContent:siteTextnckeditor.getValue(),
			                   	nisValid:siteTextnisValid.checked?1:2
		                    },
		                    success: function(fm, o){
		                    	if(o.result.success=='1'){
		                    		Ext.MessageBox.alert('信息!', '添加成功!');
			                        siteTextAddform.getForm().reset();
			                        siteTextAddWin.hide();
			                        siteTextGridStore.reload();
		                    	}else if(o.result.success=='4'){
		                    		Ext.MessageBox.alert('Error!', '用户不明确 请选择用户点击查询!');
		                    	}else{
		                    		Ext.MessageBox.alert('Error!', '添加失败!');
		                    	}
		                    },
							failure:function(form, action) {
								Ext.Msg.alert('错误', '添加失败！');
								return ;
							}
		                });
		        	}else if(this.text=='修 改'){
		        		siteTextAddform.getForm().submit({
		                    url: 'updateAdminSiteText.do',
		                    method:'POST',
		                    waitMsg: 'Uploading your data...',
		                    params : {
		                    	sTitle:siteTextTitle.getValue(),
			                   	nisValid:siteTextnisValid.checked?1:2,
			                   	sContent:siteTextnckeditor.getValue(),
			                   	nwxTextId:siteTextGridnwxTextId
		                    },
		                    success: function(fm, o){
		                    	if(o.result.success=='1'){
		                    		Ext.MessageBox.alert('信息!', '修改成功!');
			                        siteTextAddform.getForm().reset();
			                        siteTextAddWin.hide();
			                        siteTextGridStore.reload();
		                    	}else{
		                    		Ext.MessageBox.alert('Error!', '修改失败!');
		                    	}
		                    },
							failure:function(form, action) {
								Ext.Msg.alert('错误', '修改失败！');
								return ;
							}
		                });
		        	}
		        }
		    },{
		        text: '返 回',
		        handler: function() {
		        	siteTextAddform.getForm().reset();
		        	siteTextAddWin.hide();
		        }
		    }]
		});
		
		siteTextAddWin =new Ext.Window({
		    title: '信息',
		    closeAction: 'hide',
		    width: 700,
		    height: 502,
		    layout: 'fit',
	        resizable: true,
	        modal: true,
	        items: siteTextAddform,
	        listeners :{
	        	close:function(){
	        		siteTextAddform.getForm().reset();
	        	}
	    	}
		});
	}
    siteTextAddWin.show();
    
    if(tinfo==2){
    	siteTextAddWin.setTitle('信息：'+siteTextJson.stitle);
    	siteTextAddform.buttons[0].setText('修 改');
    	
    	if(siteTextJson.nisValid!=null)
    		siteTextnisValid.setValue(siteTextJson.nisValid==1?true:false);
    	if(siteTextJson.stitle!=null)
    		siteTextTitle.setValue(siteTextJson.stitle);
    	if(siteTextJson.scontent!=null)
    		siteTextnckeditor.setValue(siteTextJson.scontent);
	}else{
		siteTextAddform.buttons[0].setText('添 加');
		siteTextAddform.getForm().reset();
	}
}

Ext.onReady(function(){
	siteTextiswriteidb= Ext.get('siteTextiswriteid').getValue()==1?true:false;
	
	var siteTextnisValid=new Ext.form.ComboBox({
		fieldLabel:'是否有效',
		labelAlign:'right',
		editable:false,
        store: new Ext.data.ArrayStore({
            fields: ['value', 'text'],
            data:[
			        [1, '有效'],
			        [2, '无效']
		        ]
        }),
	    valueField:'value',
	    displayField:'text',
        mode:'local',
        typeAhead: true,
        triggerAction: 'all',
        value:1,
        selectOnFocus:true
	});
	
	var adminSiteTextnkey=new Ext.form.ComboBox({
		fieldLabel: '选择用户',
		editable:false,
        store: new Ext.data.JsonStore({
        	autoLoad:true,
            url:'selectAdminUsers.do',
            fields:['nUserId','sName']
        }),
        mode:'local',
	    valueField:'nUserId',
	    displayField:'sName',
        emptyText:'全部',
        triggerAction:'all',
        selectOnFocus:true
	});
	
	var siteTextSearchBtn=new Ext.Button({
		text:'查询',
		handler:function(){
			siteTextUserId=adminSiteTextnkey.getValue();
			siteTextGridStore.load({
				params : {
					start : 0,
					limit : 20,
					'userid':adminSiteTextnkey.getValue(),
					'ntype':siteTextnisValid.getValue()
				}
			});
		}
	});
	
	var siteTextSearch=new Ext.Panel({
		height:50,
		region:'north',
		layout:'column',
		frame: true,
		border:false,
		bodyBorder:false,
		items:[
			{columnWidth:.38,layout:'form',labelAlign:'right',items:[adminSiteTextnkey]},
			{columnWidth:.38,layout:'form',labelAlign:'right',items:[siteTextnisValid]},
			{columnWidth:.2,layout:'form',labelAlign:'right',items:[siteTextSearchBtn]}
		]
	});
	
	siteTextGridStore = new Ext.data.Store({
        autoLoad: false,
        proxy : new Ext.data.HttpProxy({
            url: 'searchAdminSiteText.do'
        }),
        baseParams:{
        	'userid':adminSiteTextnkey.getValue(),
        	'ntype':siteTextnisValid.getValue()
        },
        reader:new Ext.data.JsonReader({
             totalProperty: 'totalSize',
             root:'list'
        },[
        	{name: 'nwxTextId',mapping:'nwxTextId', type: 'int'},
            {name: 'nuserId',mapping:'nuserId', type: 'int'},
        	{name: 'nisValid',mapping:'nisValid',   type: 'int'},
        	{name: 'stitle',mapping:'stitle', type: 'string'},
        	{name: 'scontent',mapping:'scontent', type: 'string'},
        	{name: 'surl',mapping:'surl', type: 'string'},
        	{name: 'dcreateTime',mapping:'dcreateTime', type: 'date',
        		convert : function(v) {
        			return new Date(v);
        		}
        	}
        ])
    });
    
    siteTextGridStore.on("beforeload", function(thiz, options) {
		thiz.baseParams["ntype"] = siteTextnisValid.getValue();
		thiz.baseParams["userid"] = adminSiteTextnkey.getValue();
	});
    
    siteTextAddBtn =new Ext.Button({
		text:'添加',
		hidden:siteTextiswriteidb,
		handler:function(){
			showsiteTextAddForm(1,'');
		}
	});
    
    var siteTextGridBar=new Ext.PagingToolbar({
		store: siteTextGridStore,
		displayInfo: true,
		pageSize:20,
		displayMsg : '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
		emptyMsg: "没有记录可以显示",
		beforePageText:'当前页码',
		afterPageText:'共{0}页', 
		firstText:'第一页', 
		prevText:'上一页', 
		nextText:'下一页', 
		lastText:'最后一页', 
		refreshText:'刷新当前页',
		items:['-', siteTextAddBtn]
     });
     
    siteTextGridColumnsRenderInfo=function(value, cellmeta, record, rowIndex, columnIndex, store){
    	siteTextobj[record.data.nwxTextId]=record.json;
    	var linkhtml='';
    	if(siteTextiswriteidb==false){
	    	linkhtml='<a style="margin-right:20px" onclick="siteTextGridColumnsRenderInfolink('+record.data.nwxTextId+')" href="javascript:void(0)">详细 </a>';
	    	linkhtml+='<a onclick="siteTextGridColumnsRenderdellink('+record.data.nwxTextId+',\''+record.data.stitle+'\')" href="javascript:void(0)"> 删除</a>';
    	}
    	return linkhtml;
	};
	
	siteTextGrid = new Ext.grid.GridPanel({
        store: siteTextGridStore,
        region:'center',
		disableSelection: true,
		border:false,
		frame:true,
		stripeRows:true,
		loadMask :{msg:"数据加载中，请稍等..."},
		layout:'fit',
		viewConfig:{
			forceFit: true,
			scrollOffset:0
		},
        cm:new Ext.grid.ColumnModel([
        	new Ext.grid.RowNumberer(),
            {header: "标题",flex: 1, dataIndex: 'stitle', sortable: true},
            {header: "是否有效", width:50,sortable: true,dataIndex: 'nisValid',
            	renderer:function(value){
            		if (value == 1) {
			            return '有效';
			        }else{
			        	return '无效';
			        }
            	}
            },
            {header: "创建时间",flex: 1, dataIndex: 'dcreateTime', sortable: true,renderer : Ext.util.Format.dateRenderer('Y-m-d H:i:s')},
            {header: "操作", width:150,align:'center',sortable: true,renderer:siteTextGridColumnsRenderInfo}
        ]),
        bbar: siteTextGridBar
    });
    
	 var mainPanel = new Ext.Viewport({
	 	layout:'border',
		renderTo:'siteTextDIV',
		id:'siteTextMainPanel',
		items:[siteTextGrid,siteTextSearch],
		listeners: {
	        'resize': function(viewport,adjWidth,adjHeight,rawWidth,rawHeight)  {
	          	Ext.getCmp("siteTextMainPanel").setHeight((document.body.clientHeight - 290) * 0.67);  
			}
    	}
	});
})